package com.zmj.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.zmj.common.base.MyMetaObjectHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author zmj
 * @date 2021-08-16 09:03
 */
@Configuration
@MapperScan("com.zmj.mapper")//mapper接口扫描
public class MybatisPlusConfig {

    /**
     * mbp3.4.0以下版本的分页配置，3.4.0及以上版本请参考官网
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setOverflow(false);// 设置请求的页面大于最大页后操作， true调回到首页，false继续请求  默认false
        paginationInterceptor.setLimit(500);// 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));// 开启 count 的 join 优化,只针对部分 left join
        return paginationInterceptor;
    }

    /**
     * 乐观锁插件
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * 注册自动填充功能处理器
     */
    @Bean
    public MyMetaObjectHandler myMetaObjectHandler() {
        return new MyMetaObjectHandler();
    }


}
